package com.module.app.utils;

import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.module.app.arouter.path.LoginPath;
import com.module.app.arouter.service.ILoginService;
import com.module.app.bean.IType;
import com.module.app.bean.UserBean;
import com.module.app.arouter.path.LoginPath;
import com.module.app.arouter.service.ILoginService;
import com.module.app.bean.IType;
import com.module.base.cache.CacheSDK;
import com.module.base.utils.TimeUtils;
import com.module.frame.app.AppManager;

/**
 *
 */
public class UserUtils {

    public static UserBean getUserBean() {
        UserBean user = (UserBean) CacheSDK.get(IType.ICache.USER, UserBean.class);
        return user;
    }


    public static void setUserBean(UserBean userBean) {
        CacheSDK.put(IType.ICache.USER, userBean);
    }

    public static boolean isLogin() {
        if (!TextUtils.isEmpty(getUserId())) {
            return true;
        }
        return false;
    }

    public static boolean isLogin(boolean isGo) {
        if (isGo && !isLogin()) {
            ILoginService mILoginService = (ILoginService) ARouter.getInstance().build(LoginPath.S_LOGIN_SERVICE).navigation();
            if (mILoginService != null) {
                mILoginService.startLogin(AppManager.getInstance().currentActivity());
            }
            return false;
        }
        return isLogin();
    }

    public static String getUserId() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getObjectId();
        }
        return "";
    }

    public static String getPhone() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getPhone();
        }
        return "";
    }

    public static String getWx() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getWx();
        }
        return "";
    }

    public static String getQQ() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getQq();
        }
        return "";
    }

    public static String getName() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getName();
        }
        return "";
    }

    public static String getHeadUrl() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getHeadUrl();
        }
        return "";
    }

    public static boolean isVip() {
        UserBean userBean = getUserBean();
        if (userBean != null && UserUtils.isLogin()) {
            if (userBean.getVipExpiration() < 0) {
                return true;
            }
            return userBean.getVipExpiration() > System.currentTimeMillis();
        }
        return false;
    }

    public static boolean isForeverVip() {
        UserBean userBean = getUserBean();
        if (userBean != null && UserUtils.isLogin()) {
            if (userBean.getVipExpiration() == -1) {
                return true;
            }
        }
        return false;
    }

    public static String getVipExpiration() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            if (userBean.getVipExpiration() < 0) {
                return "永久";
            }
            if (userBean.getVipExpiration() == 0) {
                return "未开通";
            }
            if (userBean.getVipExpiration() < System.currentTimeMillis()) {
                return "已过期:" + TimeUtils.getDate(userBean.getVipExpiration(), "yyyy-MM-dd");
            }
            return "过期时间:" + TimeUtils.getDate(userBean.getVipExpiration(), "yyyy-MM-dd");
        }
        return "";
    }

    public static String getPushId() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getPushId();
        }
        return "";
    }

    public static @IType.ISex
    int getSex() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getSex();
        }
        return 0;
    }

    public static String getSexStr() {
        switch (getSex()) {
            case IType.ISex.male:
                return "男";
            case IType.ISex.female:
                return "女";
        }
        return "未知";
    }

    public static String getInviter() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getInviter();
        }
        return "";
    }

    public static int getInviteCount() {
        UserBean userBean = getUserBean();
        if (userBean != null) {
            return userBean.getInviteCount();
        }
        return 0;
    }
}
